package org.yxt.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.yxt.springcloud.entities.CommonResult;

@RestController
@RequestMapping(value = "sentinel")
public class SentinelController {

    /**
     * sentinel热点数据
     *
     * @param id
     * @param name
     * @return
     */
    @GetMapping(value = "hotKey")
    @SentinelResource(value = "myHotKey", blockHandler = "blockHandler")
    public CommonResult hotKey(@RequestParam(defaultValue = "0", required = false) int id, String name) {
        return new CommonResult(1, "success", "参数 id=" + id + " name=" + name);
    }

    public CommonResult blockHandler(int id, String name, BlockException exception) {
        return new CommonResult(0, "自定义sentinel 触发降级抛出BlockException 提示");
    }
}
